package org.example.threadpool;

import lombok.extern.slf4j.Slf4j;
import org.example.utils.Sleeper;

import java.util.concurrent.SynchronousQueue;

/**
 * @author zhengdp
 * @Description 队列测试
 * @Date 2022/12/11 22:17
 * @Created by zhengdp
 */
@Slf4j(topic = "c.SynchronousQueueTest")
public class SynchronousQueueTest {
    public static void main(String[] args) {
        SynchronousQueue<Integer> queue = new SynchronousQueue<>();
        new Thread(() -> {
            try {
                log.info("putting {}" ,1);
                queue.put(1);;
                log.info("{} putted ...",1);

                log.info("putting {}" ,2);
                queue.put(2);;
                log.info("{} putted ...",2);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"t1").start();

        Sleeper.sleep(1000);

        new Thread(() -> {
            try{
                log.info("taking {} ",1);
                queue.take();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"t2").start();

        Sleeper.sleep(1000);

        new Thread(() -> {
            try{
                log.info("taking {} ",2);
                queue.take();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"t3").start();
    }
}
